package com.len.controller;

import com.len.base.BaseController;
import com.len.core.annotation.Log;
import com.len.entity.Behavior;
import com.len.entity.Channel;
import com.len.exception.MyException;
import com.len.service.BehaviorService;
import com.len.service.ChannelService;
import com.len.util.BeanUtil;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author songxiangyang
 * @date 2019/03/25.
 * 渠道业务
 */
@Controller
@RequestMapping(value = "/channel")
public class ChannelController extends BaseController {

    @Autowired
    private ChannelService channelService;
    @Autowired
    private BehaviorService behaviorService;

    @GetMapping(value = "showChannel")
    @RequiresPermissions(value = "channel:show")
    public String showChannel(Model model) {
        return "channel/channelList";
    }

    @ApiOperation(value = "/showChannelList", httpMethod = "GET", notes = "展示渠道")
    @GetMapping(value = "showChannelList")
    @ResponseBody
    @RequiresPermissions("channel:show")
    public ReType showChannelList(Channel channel, Model model, String page, String limit) {
        return channelService.show(channel, Integer.valueOf(page), Integer.valueOf(limit));
    }

    @GetMapping(value = "showAddChannel")
    public String goAddChannel(Model model) {
        return "channel/add-channel";
    }

    @ApiOperation(value = "/addChannel", httpMethod = "POST", notes = "添加渠道")
    @Log(desc = "添加渠道")
    @PostMapping(value = "addChannel")
    @ResponseBody
    public JsonUtil addChannel(Channel channel) {
        JsonUtil j = new JsonUtil();
        try {
            channelService.insertSelective(channel);
            j.setMsg("保存成功");
        } catch (MyException e) {
            j.setMsg("保存失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

    @GetMapping(value = "updateChannel")
    public String updateChannel(String id, Model model, boolean detail) {
        if (StringUtils.isNotEmpty(id)) {
            Channel channel = channelService.selectByPrimaryKey(id);
            model.addAttribute("channel", channel);
        }
        model.addAttribute("detail", detail);
        return "channel/update-channel";
    }

    @ApiOperation(value = "/updateChannel", httpMethod = "POST", notes = "更新渠道")
    @Log(desc = "更新渠道")
    @PostMapping(value = "updateChannel")
    @ResponseBody
    public JsonUtil updateChannel(Channel channel) {
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setFlag(false);
        if (channel == null) {
            jsonUtil.setMsg("获取数据失败");
            return jsonUtil;
        }
        try {
            Channel oldChannel = channelService.selectByPrimaryKey(channel.getId());
            BeanUtil.copyNotNullBean(channel, oldChannel);
            channelService.updateByPrimaryKeySelective(oldChannel);

            jsonUtil.setFlag(true);
            jsonUtil.setMsg("修改成功");
        } catch (MyException e) {
            jsonUtil.setMsg("修改失败");
            e.printStackTrace();
        }
        return jsonUtil;
    }

    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除渠道")
    @Log(desc = "删除渠道", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "del")
    @ResponseBody
    @RequiresPermissions("channel:del")
    public JsonUtil del(String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonUtil.error("获取数据失败");
        }
        Behavior behavior = new Behavior();
        behavior.setChannelId(id);
        JsonUtil j = new JsonUtil();
        try {
            int count = behaviorService.selectCountByCondition(behavior);
            if (count > 0) {
                return JsonUtil.error("已分配给行为，删除失败");
            }
            channelService.deleteByPrimaryKey(id);
            j.setMsg("删除成功");
        } catch (MyException e) {
            j.setMsg("删除失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

}
